﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace MyLib
{
    public class clsStatic
    {
        public static string getAppSettingsString(string sKey)
        {
            return clsStatic.getAppSettingsString(sKey, "", false);
        }

        public static string getAppSettingsString(string sKey, string sDefault)
        {
            return getAppSettingsString(sKey, sDefault,false);
        }

        public static string getAppSettingsString(string sKey, string sDefault, bool CreateIfNotExists)
        {
            var apps = ConfigurationManager.AppSettings[sKey];
            if (apps == null)
            {
                if (CreateIfNotExists)
                {
                    ConfigurationManager.AppSettings.Add(sKey, sDefault);
                }
                return sDefault;
            }
            return apps;
        }



        public static System.Configuration.Configuration WebConfig
        {
            get
            {
                System.Configuration.Configuration config =
                  System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

                return config;
            }
        }

        private static string _connectionString = "";
        public static String DataConnectionString
        {
            get
            {
                if (_connectionString != "")
                {
                    return _connectionString;
                }

                _connectionString = "DiagnosticsConnectionString";

                if (IsDevFabric())
                {
                    _connectionString += "Local";
                }
                else
                {
                    _connectionString += "Remote";
                }

                return _connectionString;
            }
        }

        public static bool IsDevFabric()
        {
            bool isDevFabric = true;


            isDevFabric = (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("_DF_STATE_DIRECTORY")));

            isDevFabric = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.DeploymentId.StartsWith("deployment(");

            return isDevFabric;
        }
    }
}
